package com.leet.code;

import java.util.LinkedList;
import java.util.List;

public class Code22 {


    public static void main(String[] args) {
        List<String> l = new Code22().generateParenthesis(1);
        System.out.println(l);
    }

    List<String> list = new LinkedList<>();

    public List<String> generateParenthesis(int n) {
        char[] chars = new char[(n << 1)];
        dfs(0, 0, n, chars);
        return list;
    }

    public void dfs(int l, int r, int n, char[] cs) {
        if ((l + r) == cs.length) {
            list.add(new String(cs));
            return;
        }

        if (l < n) {
            cs[l + r] = '(';
            dfs(l + 1, r, n, cs);
        }

        if (r < l) {
            cs[l + r] = ')';
            dfs(l, r + 1, n, cs);
        }


    }

}
